/*
 * @Author: hkk 2812877145@qq.com
 * @Date: 2023-05-23 13:13:03
 * @LastEditors: hkk 2812877145@qq.com
 * @LastEditTime: 2023-05-23 15:49:07
 * @FilePath: \hello-word\src\unit\music_public_fn\index.js
 * @Description: 歌词公共方法
 * Copyright (c) 2023 by hkk, All Rights Reserved. 
 */

import notify from '../error/index'

/**
 * @Date: 2023-05-23 13:59:44
 * @description: 将字符串数组格式化为对象数组
 * @param {*} data
 * @return {*}
 */
export function getCutlyrics(data) {
  if(data.length < 1) return;
  return data.map((e) => {
    return splitLyric(e);
  })
  .filter(Boolean)
}

/**
 * @Date: 2023-05-23 14:04:13
 * @description: 格式化每一项
 * @param {*} item
 * @return {*}
 */
export function splitLyric(item) {
  const Lyric = item.split("]");
  const timerLyric = Lyric[0].substr(1);
  let timer = timerLyric.split(":");
  let second = parseInt(timer[0]);
  if(!Lyric[1]){
    return;
  }
  if (isNaN(second)) {
    return { timer: timerLyric, text: Lyric[1] };
  } else {
    second = (second * 60 + +timer[1]).toFixed(2);
    return { timer: second, text: Lyric[1] };
  }
}

/**
 * @Date: 2023-05-23 14:36:16
 * @description: 传入歌词数组，创建歌词文本元素，设置类名
 * @param {*} lrcData
 * @param {*} lrcItemClassName
 * @return {*}
 */
export function initLrcUlText(lrcData, lrcItemClassName){
  if(!Array.isArray(lrcData)){
    notify('error','调用initLrcUlText()方法的第一个参数应该为数组')
    throw new Error('调用initLrcUlText()方法的第一个参数应该为数组')
  }
  // 创建一个dom文档对象,这个文档对象是脱离dom树的;
  const textLi = document.createDocumentFragment();
  lrcData.forEach((e) => {
    const li = document.createElement("li");
    li.textContent = e.text;
    li.classList.add(lrcItemClassName);
    textLi.appendChild(li);
  });
  return textLi;
}